/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.form.item.ListItem;
import com.jie.eclipse.pan.form.support.FormItem;
import com.jie.eclipse.pan.listener.PJumpCaptionModifyListener;
import com.jie.eclipse.pan.listener.PModifyListener;
import com.jie.eclipse.pan.utils.StringBuilderEx;

/**
 * @author yanjie
 *
 */
public class ListItemDialog extends BaseDialog{

	/**
	 * @param parentShell
	 * @param data
	 * @param action
	 */
	public ListItemDialog(Shell parentShell, BaseDataEntity data, String action) {
		super(parentShell, data, action);
	}
	
	@Override
	protected Control createDialogArea(Composite paramComposite) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		Composite composite = new Composite(paramComposite, SWT.NONE);
		composite.setLayout(new GridLayout(4, false));
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		composite.setBackground(bgcolor);
		Label lb = null;
		GridData gd = null;
		
//		lb=  new Label(composite,SWT.RIGHT);
//		lb.setText("ID:");
//		lb.setBackground(bgcolor);
//		Text id = new Text(composite, SWT.BORDER);
//		id.setLayoutData(new GridData(200, 15));
//		this.getDataBind().bind("id", id);	
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("名稱:");
		lb.setBackground(bgcolor);
		Text name = new Text(composite, SWT.BORDER);
		name.setLayoutData(new GridData(120, 15));
		this.getDataBind().bind("name", name);
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("标题:");
		lb.setBackground(bgcolor);
		Text caption = new Text(composite, SWT.BORDER);
		caption.setLayoutData(new GridData(120, 15));
		this.getDataBind().bind("caption", caption);
		
		name.addModifyListener(new PModifyListener("commonChange",null));
		name.addFocusListener(new PJumpCaptionModifyListener(caption));
		

		lb=  new Label(composite,SWT.RIGHT);
//		lb.setLayoutData(fh);
		lb.setText("列数:");
		lb.setBackground(bgcolor);
		Text colspan = new Text(composite, SWT.BORDER);
		gd = new GridData(120, 15);
		gd.horizontalSpan=3;
		colspan.setLayoutData(gd);
		this.getDataBind().bind("colspan", colspan);
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("为空:");
		lb.setBackground(bgcolor);
		Combo required = new Combo(composite, SWT.READ_ONLY);
		required.setItems(new String[]{"Y","N"});
		required.select(0);
		required.setLayoutData(new GridData(100, 24));
		required.setData("0","false");
		required.setData("1","true");
		this.getDataBind().bind("required", required);
		
		if(this instanceof CheckboxDialog){
			lb=  new Label(composite,SWT.RIGHT);
			lb.setText("是否帶隐藏框:");
			lb.setBackground(bgcolor);
			
			Button withHidden = new Button(composite, SWT.CHECK);
			withHidden.setBackground(bgcolor);
			withHidden.setData(true);
			this.getDataBind().bind("withHidden", withHidden);
		}else{
			lb=  new Label(composite,SWT.RIGHT);
			gd = new GridData();
			gd.horizontalSpan=2;
			lb.setLayoutData(gd);
		}
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("静态代码表:");
		lb.setBackground(bgcolor);
		Text codeList = new Text(composite, SWT.BORDER);
		gd = new GridData(300, 15);
		gd.horizontalSpan=3;
		codeList.setLayoutData(gd);
		this.getDataBind().bind("codeList", codeList);
		
		lb = new Label(composite, SWT.RIGHT);
		lb.setText("数据源:");
		Combo dataSource = new Combo(composite, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(true);
		dataSource.setItems(datasources);
		dataSource.select(0);
		dataSource.setLayoutData(new GridData(100, 24));
		dataSource.setData("0", "");
		for (int i = 0; i < datasources.length; i++) {
			dataSource.setData(String.valueOf(i+1), datasources[i]);
		}
		
		gd = new GridData(280, 15);
		gd.horizontalSpan=3;
		dataSource.setLayoutData(gd);
		this.getDataBind().bind("dataSource", dataSource);
		
		lb = new Label(composite, SWT.RIGHT);
		lb.setText("SQl:");
		Text sql = new Text(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 100;
		gd.horizontalSpan=3;
		sql.setLayoutData(gd);
		this.getDataBind().bind("sql", sql);
		
		appendExtComposite(composite);
		
		this.reloadRecord();
		return paramComposite;
	}
	
	public void appendExtComposite(Composite parent){
		
	}
	
	@Override
	public void initRootItem(BaseDataEntity data,BaseDataBind bind,StringBuilderEx code) {
		if(bind == null)return ;
		BaseDataEntity config = bind.getRecord();
		FormItem item = getNewFormItem();
		item.configItem(config,"default");
		this.setRoot(item);
	}
	
	@Override
	public void fillConfig(BaseDataEntity config, BaseDataBind dataBind) {
		super.fillConfig(config, dataBind);
		BaseDataEntity record = dataBind.getRecord();
		record.CopyTo(config,"codeList,dataSource,sql",false);
	}
	
	@Override
	protected Point getInitialSize() {
		 Point point = super.getInitialSize();
		 point.x = 500;
		return point;
	}
}
